package com.stock.stockana.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.stock.stockana.beans.StockInfoBean;
import com.stock.stockana.ui.activities.MainActivity;
import com.stock.stockana.R;
import com.stock.stockana.StockCenter;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.GetRecStocksEvent;
import com.stock.stockana.requests.GetRecStocks;
import com.stock.stockana.ui.activities.StockDetailActivity;
import com.stock.stockana.ui.adapter.RecomStockAdapter;
import com.stock.stockana.ui.listener.OnStockItemClickedListener;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * ---------------------------------------------------
 * Description: 帅选看股fragment页面适配器
 * Author: ljgsonx
 * Belong to: com.stock.stockana.fragments
 * Time: 2016/1/20 18:41
 * ---------------------------------------------------
 */
public class RecomStockFragment extends Fragment {

    private final static String TAG = "RecomStockFragment";
    private View mView;
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private RecomStockAdapter mRecomStockAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.recyclerview, container, false);
        mRecyclerView = (RecyclerView) mView.findViewById(com.stock.publiclib.R.id.id_recyclerview);
        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecomStockAdapter = new RecomStockAdapter(getActivity());
        mRecomStockAdapter.setmOnStockItemClickedListener(new OnStockItemClickedListener() {
            @Override
            public void onStockItemCliked(StockInfoBean clickedStock) {
                Intent intent = new Intent(getActivity(), StockDetailActivity.class);
                intent.putExtra("clickedStock", clickedStock);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left);
            }
        });
        mRecyclerView.setAdapter(mRecomStockAdapter);
        mRecyclerView.setLayoutManager(mLayoutManager);
        StockCenter.doReq(GetRecStocks.build());
        return mView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * Description: 数据获取到之后,刷新适配器
     * Author: ljgsonx
     * Time: 2016/1/23 18:57
     */
    @Subscribe
    public void onEventMainThread(GetRecStocksEvent event){
        ((MainActivity)getActivity()).stopPopLoading();
        mRecomStockAdapter.allowOpenStockSelector();
        if(event.getResponseCode() == NetWorkConstants.RESPONSE_CODE_OK){
            //由于无数据的提示,必须要先调用
            mRecomStockAdapter.notifyDataSetChanged();
        }else {
            Log.d(TAG, event.getMessage());
            Toast.makeText(getActivity(), NetWorkConstants.RESPONSE_COMMON_MESSAGE, Toast.LENGTH_LONG).show();
        }
    }
}
